﻿using System;
using System.Data;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web.UI.WebControls;
using TraditionalMalteseCuisine.App_Code.Objects;
using TraditionalMalteseCuisine.App_Code.Operations;

namespace TraditionalMalteseCuisine.App_Code.Functions
{
    public class Site_Functions
    {
        #region Constants
        public const char SPLITTER = '|';
        #endregion



        #region Check Methods
        public static bool DoesUsernameExists(string username)
        {
            DataTable dt = ReadOp.GetDataTable(SiteMember.GetCommand_GetRecordsBy(SiteMember.TABLE_NAME, SiteMember.USERNAME_TAG, username));

            return ((dt != null) && (dt.Rows.Count > 0));
        }

        public static bool DoesEmailExists(string email)
        {
            DataTable dt = ReadOp.GetDataTable(SiteMember.GetCommand_GetRecordsBy(SiteMember.TABLE_NAME, SiteMember.EMAIL_TAG, email));

            return ((dt != null) && (dt.Rows.Count > 0));
        }

        public static bool DoesTagNameExists(string tagName)
        {
            DataTable dt = ReadOp.GetDataTable(Tag.GetCommand_GetRecordsBy(Tag.TABLE_NAME, Tag.TAG_NAME_TAG, tagName));

            return ((dt != null) && (dt.Rows.Count > 0));
        }

        public static bool DoesRecipeTagExists(RecipeTag recipeTag)
        {
            string[] fieldnames = string.Format("{1}{0}{2}", SPLITTER, RecipeTag.RECIPE_ID_TAG, RecipeTag.TAG_ID_TAG).Split(SPLITTER);
            string[] values = string.Format("{1}{0}{2}", SPLITTER, recipeTag.RecipeID, recipeTag.TagID).Split(SPLITTER);

            DataTable dt = ReadOp.GetDataTable(RecipeTag.GetCommand_GetRecordsBy(RecipeTag.TABLE_NAME, fieldnames, values));

            return ((dt != null) && (dt.Rows.Count > 0));
        }

        public static bool DoesRecipeLikeExists(RecipeLike recipeLike)
        {
            string[] fieldnames = string.Format("{1}{0}{2}", SPLITTER, RecipeLike.RECIPE_ID_TAG, RecipeLike.MEMBER_ID_TAG).Split(SPLITTER);
            string[] values = string.Format("{1}{0}{2}", SPLITTER, recipeLike.RecipeID, recipeLike.MemberID).Split(SPLITTER);

            DataTable dt = ReadOp.GetDataTable(RecipeTag.GetCommand_GetRecordsBy(RecipeLike.TABLE_NAME, fieldnames, values));

            return ((dt != null) && (dt.Rows.Count > 0));
        }

        public static bool DoesSubscriptionExists(Subscription subscription)
        {
            string[] fieldnames = string.Format("{1}{0}{2}", SPLITTER, Subscription.MEMBER_ID_TAG, Subscription.SUBSCRIBED_TO_MEMBER_ID_TAG).Split(SPLITTER);
            string[] values = string.Format("{1}{0}{2}", SPLITTER, subscription.MemberID, subscription.SubscribedToMemberID).Split(SPLITTER);

            DataTable dt = ReadOp.GetDataTable(RecipeTag.GetCommand_GetRecordsBy(Subscription.TABLE_NAME, fieldnames, values));

            return ((dt != null) && (dt.Rows.Count > 0));
        }
        #endregion



        #region Get Methods
        public static Tag GetTagByID(int tagID)
        {
            Tag tag = null;

            DataTable dt = ReadOp.GetDataTable(Tag.GetCommand_GetRecordsBy(Tag.TABLE_NAME, Tag.ID_TAG, tagID.ToString()));

            if (dt.Rows.Count == 1)
                tag = (Tag)(new Tag()).ReadDataRow(dt.Rows[0]);

            return tag;
        }

        public static Tag GetTagByTagName(string tagNmae)
        {
            Tag tag = null;

            DataTable dt = ReadOp.GetDataTable(Tag.GetCommand_GetRecordsBy(Tag.TABLE_NAME, Tag.TAG_NAME_TAG, tagNmae.ToString()));

            if (dt.Rows.Count == 1)
                tag = (Tag)(new Tag()).ReadDataRow(dt.Rows[0]);

            return tag;
        }

        public static Level GetLevelByID(int levelID)
        {
            Level level = null;

            DataTable dt = ReadOp.GetDataTable(Level.GetCommand_GetRecordsBy(Level.TABLE_NAME, Level.ID_TAG, levelID.ToString()));

            if (dt.Rows.Count == 1)
                level = (Level)(new Level()).ReadDataRow(dt.Rows[0]);

            return level;
        }

        public static Recipe GetRecipeByID(int recipeID)
        {
            Recipe recipe = null;

            DataTable dtRecipes = ReadOp.GetDataTable(Recipe.GetCommand_GetRecordsBy(Recipe.TABLE_NAME, Recipe.ID_TAG, recipeID.ToString()));

            if ((dtRecipes != null) && (dtRecipes.Rows.Count == 1))
                recipe = (Recipe)(new Recipe()).ReadDataRow(dtRecipes.Rows[0]);

            return recipe;
        }

        public static SiteMember GetSiteMemberByID(int memberID)
        {
            SiteMember member = null;

            DataTable dt = ReadOp.GetDataTable(SiteMember.GetCommand_GetRecordsBy(SiteMember.TABLE_NAME, SiteMember.ID_TAG, memberID.ToString()));

            if (dt.Rows.Count == 1)
                member = (SiteMember)(new SiteMember()).ReadDataRow(dt.Rows[0]);

            return member;
        }

        public static RecipeRating GetRecipeRating(int recipeID, int memberID)
        {
            RecipeRating recipeRating = null;

            string[] fieldnames = string.Format("{1}{0}{2}", SPLITTER, RecipeRating.RECIPE_ID_TAG, RecipeRating.MEMBER_ID_TAG).Split(SPLITTER);
            string[] values = string.Format("{1}{0}{2}", SPLITTER, recipeID, memberID).Split(SPLITTER);

            DataTable dtRecipesRatings = ReadOp.GetDataTable(RecipeRating.GetCommand_GetRecordsBy(RecipeRating.TABLE_NAME, fieldnames, values));

            if ((dtRecipesRatings != null) && (dtRecipesRatings.Rows.Count == 1))
                recipeRating = (RecipeRating)(new RecipeRating()).ReadDataRow(dtRecipesRatings.Rows[0]);

            return recipeRating;
        }


        public static SiteMember GetSiteMemberDetailsUsingEMail(string email)
        {
            SiteMember member = null;

            DataTable dt = ReadOp.GetDataTable(SiteMember.GetCommand_GetRecordsBy(SiteMember.TABLE_NAME, SiteMember.EMAIL_TAG, email));

            if (dt.Rows.Count == 1)
            {
                member = (SiteMember)(new SiteMember()).ReadDataRow(dt.Rows[0]);

                DateTime lastLogin = GetLastLoginDate(member);

                if (lastLogin != DateTime.MinValue)
                    member.LastLogin = string.Format("{0:dd-MMM-yyyy HH:mm:ss}", lastLogin);
            }

            return member;
        }

        public static int GetRecipeID(Recipe recipe)
        {
            string[] fieldnames = string.Format("{1}{0}{2}", SPLITTER, Recipe.RECIPE_NAME_TAG, Recipe.DESCRIPTION_TAG, Recipe.DIRECTIONS_TAG, Recipe.IMAGE_LOCATION_TAG, Recipe.THUMBNAIL_LOCATION_TAG).Split(SPLITTER);
            string[] values = string.Format("{1}{0}{2}", SPLITTER, recipe.RecipeName, recipe.Description, recipe.Directions, recipe.ImageLocation, recipe.ThumbnailLocation).Split(SPLITTER);

            DataTable dt = ReadOp.GetDataTable(Recipe.GetCommand_GetRecordsBy(Recipe.TABLE_NAME, fieldnames, values));

            if (dt.Rows.Count == 1)
                recipe = (Recipe)(new Recipe()).ReadDataRow(dt.Rows[0]);

            return recipe.ID;
        }

        public static int GetTagID(Tag tag)
        {
            string[] fieldnames = string.Format("{1}{0}{2}", SPLITTER, Tag.PARENT_ID_TAG, Tag.TAG_NAME_TAG).Split(SPLITTER);
            string[] values = string.Format("{1}{0}{2}", SPLITTER, tag.ParentID, tag.TagName).Split(SPLITTER);

            DataTable dt = ReadOp.GetDataTable(Tag.GetCommand_GetRecordsBy(Tag.TABLE_NAME, fieldnames, values));

            if (dt.Rows.Count == 1)
                tag = (Tag)(new Tag()).ReadDataRow(dt.Rows[0]);

            return tag.ID;
        }


        public static string GetRatingStatistics(Recipe recipe)
        {
            double averageRating = 0;
            int totalVotes = 0;

            DataTable dt = ReadOp.GetDataTable(RecipeRating.GetCommand_GetRecordsBy(RecipeRating.VIEW_NAME_ALL_RECIPES_RATINGS_AVERAGES, RecipeRating.RECIPE_ID_TAG, recipe.ID.ToString()));

            if ((dt != null) && (dt.Rows.Count == 1))
            {
                averageRating = double.Parse(dt.Rows[0][RecipeRating.AVERAGE_RATING_TAG].ToString());
                totalVotes = int.Parse(dt.Rows[0][RecipeRating.TOTAL_VOTES_TAG].ToString());
            }

            return string.Format("{0} of 5 ({1} votes)", Math.Round(averageRating, 2), totalVotes);
        }

        public static string GetLikesStatistics(Recipe recipe)
        {
            int totalLikes = 0;

            DataTable dt = ReadOp.GetDataTable(RecipeRating.GetCommand_GetRecordsBy(RecipeLike.VIEW_NAME_RECIPES_LIKES_TOTAL_LIKES, RecipeLike.RECIPE_ID_TAG, recipe.ID.ToString()));

            if ((dt != null) && (dt.Rows.Count == 1))
                totalLikes = int.Parse(dt.Rows[0][RecipeLike.TOTAL_LIKES_TAG].ToString());

            return string.Format("{0} member(s) like this!", totalLikes);
        }

        public static string GetSubscriptionStatistics(Subscription subscription)
        {
            int totalSubscriptions = 0;

            DataTable dt = ReadOp.GetDataTable(Subscription.GetCommand_GetRecordsBy(Subscription.VIEW_NAME_SUBSCRIPTIONS_TOTAL_SUBSCRIPTIONS, Subscription.SUBSCRIBED_TO_MEMBER_ID_TAG, subscription.SubscribedToMemberID.ToString()));

            if ((dt != null) && (dt.Rows.Count == 1))
                totalSubscriptions = int.Parse(dt.Rows[0][Subscription.TOTAL_SUBSCRIPTIONS_TAG].ToString());

            return string.Format("{0} member(s) subscribed!", totalSubscriptions);
        }

        public static int GetMaxNumberOfPosts()
        {
            int total = 0;

            DataTable dt = ReadOp.GetDataTable(SiteMember.GetCommand_GetTopContributorsInRecipesPosted(1));

            if ((dt != null) && (dt.Rows.Count == 1))
                total = int.Parse(dt.Rows[0][SiteMember.TOTAL_RECIPES_POSTED_TAG].ToString());

            return total;
        }

        public static int GetTotalNumberOfRecipesPosted()
        {
            DataTable dtAllRecipes = ReadOp.GetDataTable(Recipe.GetCommand_GetAllRecords(Recipe.TABLE_NAME));

            return ((dtAllRecipes == null) ? 0 : dtAllRecipes.Rows.Count);
        }

        public static int GetTotalNumberOfRecipesPosted(SiteMember member)
        {
            DataTable dtRecipes = ReadOp.GetDataTable(Recipe.GetCommand_GetRecordsBy(Recipe.TABLE_NAME, Recipe.MEMBER_ID_TAG, member.ID.ToString()));

            return ((dtRecipes == null) ? 0 : dtRecipes.Rows.Count);
        }

        public static int GetTotalNumberOfRecipesCommentsPosted()
        {
            DataTable dtAllRecipesComments = ReadOp.GetDataTable(RecipeComment.GetCommand_GetAllRecords(RecipeComment.TABLE_NAME));

            return ((dtAllRecipesComments == null) ? 0 : dtAllRecipesComments.Rows.Count);
        }

        public static int GetTotalNumberOfRecipesCommentsPosted(SiteMember member)
        {
            DataTable dtRecipesComments = ReadOp.GetDataTable(RecipeComment.GetCommand_GetRecordsBy(RecipeComment.TABLE_NAME, RecipeComment.MEMBER_ID_TAG, member.ID.ToString()));

            return ((dtRecipesComments == null) ? 0 : dtRecipesComments.Rows.Count);
        }

        public static DateTime GetLastLoginDate(SiteMember member)
        {
            ActivityLog lastActivity = null;

            DataTable dt = ReadOp.GetDataTable(ActivityLog.GetCommand_GetRecordsBy(ActivityLog.VIEW_NAME_ALL_ACTIVITY_LOG_LAST_LOGIN, ActivityLog.MEMBER_ID_TAG, member.ID.ToString()));

            if (dt.Rows.Count == 1)
                lastActivity = (ActivityLog)(new ActivityLog()).ReadDataRow(dt.Rows[0]);

            return ((lastActivity == null) ? DateTime.MinValue : lastActivity.Activity_Date);
        }
        #endregion



        #region Add Methods
        public static bool AddSiteMember(SiteMember member)
        {
            return WriteOp.ExecuteCommand(member.GetInsertCommand());
        }

        public static bool AddSubscription(Subscription subscription)
        {
            RemoveSubscription(subscription);

            return WriteOp.ExecuteCommand(subscription.GetInsertCommand());
        }

        public static bool AddRecipe(Recipe recipe)
        {
            bool success = false;

            success = WriteOp.ExecuteCommand(recipe.GetInsertCommand());

            if (success)
            {
                success = false;

                recipe.ID = GetRecipeID(recipe);

                foreach (RecipeTag recipeTag in recipe.RecipesTags)
                {
                    recipeTag.RecipeID = recipe.ID;
                    if (!DoesRecipeTagExists(recipeTag))
                        Site_Functions.AddRecipeTag(recipeTag);
                }

                success = true;
            }

            return success;
        }

        public static bool AddTag(Tag tag)
        {
            return WriteOp.ExecuteCommand(tag.GetInsertCommand());
        }

        public static bool AddRecipeTag(RecipeTag recipeTag)
        {
            RemoveRecipeTag(recipeTag);

            return WriteOp.ExecuteCommand(recipeTag.GetInsertCommand());
        }

        public static bool AddRecipeLike(RecipeLike recipeLike)
        {
            RemoveRecipeLike(recipeLike);

            return WriteOp.ExecuteCommand(recipeLike.GetInsertCommand());
        }

        public static bool AddRecipeRating(RecipeRating recipeRating)
        {
            return WriteOp.ExecuteCommand(recipeRating.GetInsertCommand());
        }

        public static bool AddRecipeComment(RecipeComment recipeComment)
        {
            return WriteOp.ExecuteCommand(recipeComment.GetInsertCommand());
        }


        public static void InsertInActivityLog(int memberID, string activity, string description)
        {
            InsertInActivityLog(new ActivityLog(memberID, activity, description));
        }

        public static void InsertInActivityLog(ActivityLog activityLog)
        {
            WriteOp.ExecuteCommand(activityLog.GetInsertCommand());
        }
        #endregion



        #region Update Methods
        public static bool UpdateSiteMember(SiteMember member, string fieldname, string newValue)
        {
            return WriteOp.ExecuteCommand(member.GetUpdateCommand(SiteMember.TABLE_NAME, fieldname, newValue));
        }

        public static bool UpdateSiteMember(SiteMember member, string[] fieldnames, string[] newValues)
        {
            return WriteOp.ExecuteCommand(member.GetUpdateCommand(SiteMember.TABLE_NAME, fieldnames, newValues));
        }


        public static bool UpdateRecipe(Recipe recipe, string fieldname, string newValue)
        {
            return WriteOp.ExecuteCommand(recipe.GetUpdateCommand(Recipe.TABLE_NAME, fieldname, newValue));
        }

        public static bool UpdateRecipe(Recipe recipe, string[] fieldnames, string[] newValues)
        {
            return WriteOp.ExecuteCommand(recipe.GetUpdateCommand(Recipe.TABLE_NAME, fieldnames, newValues));
        }
        #endregion



        #region Delete Methods
        public static bool ClearTagsForRecipeID(int recipeID)
        {
            string commandText = string.Format("DELETE FROM {0} WHERE ({1} = {2})", RecipeTag.TABLE_NAME, RecipeTag.RECIPE_ID_TAG, recipeID);

            return WriteOp.ExecuteCommand(commandText);
        }

        public static bool RemoveRecipeComment(int recipeCommentID)
        {
            string commandText = string.Format("DELETE FROM {0} WHERE ({1} = {2})", RecipeComment.TABLE_NAME, RecipeComment.ID_TAG, recipeCommentID);

            return WriteOp.ExecuteCommand(commandText);
        }

        public static bool RemoveRecipeTag(RecipeTag recipeTag)
        {
            string commandText = string.Format("DELETE FROM {0} WHERE ({1} = {2}) AND ({3} = {4})", RecipeTag.TABLE_NAME, RecipeTag.RECIPE_ID_TAG, recipeTag.RecipeID, RecipeTag.TAG_ID_TAG, recipeTag.TagID);

            return WriteOp.ExecuteCommand(commandText);
        }

        public static bool RemoveRecipeLike(RecipeLike recipeLike)
        {
            string commandText = string.Format("DELETE FROM {0} WHERE ({1} = {2}) AND ({3} = {4})", RecipeLike.TABLE_NAME, RecipeLike.RECIPE_ID_TAG, recipeLike.RecipeID, RecipeLike.MEMBER_ID_TAG, recipeLike.MemberID);

            return WriteOp.ExecuteCommand(commandText);
        }

        public static bool RemoveRecipeRating(RecipeRating recipeRating)
        {
            string commandText = string.Format("DELETE FROM {0} WHERE ({1} = {2}) AND ({3} = {4})", RecipeRating.TABLE_NAME, RecipeRating.RECIPE_ID_TAG, recipeRating.RecipeID, RecipeRating.MEMBER_ID_TAG, recipeRating.MemberID);

            return WriteOp.ExecuteCommand(commandText);
        }

        public static bool RemoveSubscription(Subscription subscription)
        {
            string commandText = string.Format("DELETE FROM {0} WHERE ({1} = {2}) AND ({3} = {4})", Subscription.TABLE_NAME, Subscription.MEMBER_ID_TAG, subscription.MemberID, Subscription.SUBSCRIBED_TO_MEMBER_ID_TAG, subscription.SubscribedToMemberID);

            return WriteOp.ExecuteCommand(commandText);
        }
        #endregion



        #region Utility Methods
        public static SiteMember DoLogin(string username, string password)
        {
            SiteMember member = null;

            string[] fieldnames = string.Format("{1}{0}{2}", SPLITTER, SiteMember.USERNAME_TAG, SiteMember.PASSWORD_TAG).Split(SPLITTER);
            string[] values = string.Format("{1}{0}{2}", SPLITTER, username, password).Split(SPLITTER);

            DataTable dt = ReadOp.GetDataTable(SiteMember.GetCommand_GetRecordsBy(SiteMember.TABLE_NAME, fieldnames, values));

            if (dt.Rows.Count == 1)
            {
                member = (SiteMember)(new SiteMember()).ReadDataRow(dt.Rows[0]);

                DateTime lastLogin = GetLastLoginDate(member);

                if (lastLogin != DateTime.MinValue)
                    member.LastLogin = string.Format("{0:dd-MMM-yyyy HH:mm:ss}", lastLogin);

                Site_Functions.InsertInActivityLog(member.ID, ActivityLog.LOGIN_ACTION, "User logged in");
            }

            return member;
        }

        public static string GetAwardLevelForRecipesPosted(SiteMember member)
        {
            return GetBadgeAwards(member, GetTotalNumberOfRecipesPosted(member));
        }

        public static string GetAwardLevelForRecipesCommentsPosted(SiteMember member)
        {
            return GetAwardLevel(member, GetTotalNumberOfRecipesCommentsPosted(member));
        }

        public static string GetAwardLevel(SiteMember member, int totalPosts)
        {
            const string AWARD = "&nbsp;&nbsp;<img src=\"{0}\" height=\"20\" class=\"img_no_border\" alt=\"{1} of 5 Score\" title=\"{1} of 5 Score\" />";

            StringBuilder awards = new StringBuilder();

            int maxPosts = GetMaxNumberOfPosts();

            if (maxPosts > 0)
            {
                double percantageLevel = ((totalPosts * 100) / maxPosts);

                double contributionLevel = (percantageLevel * 5) / 100;

                if (contributionLevel == 5D)
                    awards.AppendFormat(string.Format("{0}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", AWARD), "SiteContentImages/TopAward.png", "Superb").Replace(" of 5", string.Empty);

                double totalAwards = Math.Floor(contributionLevel);

                if (totalAwards < 1D)
                    awards.AppendFormat(AWARD, "SiteContentImages/NoAward.png", Math.Round(contributionLevel, 2));
                else
                    for (int i = 0; i < totalAwards; i++)
                        awards.AppendFormat(AWARD, "SiteContentImages/Award.png", Math.Round(contributionLevel, 2));
            }
            else
            {
                awards.AppendFormat(AWARD, "SiteContentImages/NoAward.png", maxPosts);
            }

            return awards.ToString();
        }

        public static string GetBadgeAwards(SiteMember member, int totalPosts)
        {
            const string AWARD = "&nbsp;&nbsp;<img src=\"{0}\" height=\"35\" class=\"img_no_border\" alt=\"{1} of 5 Score\" title=\"{1} of 5 Score\" />";

            StringBuilder awards = new StringBuilder();

            int maxPosts = GetMaxNumberOfPosts();

            if (maxPosts > 0)
            {
                double percantageLevel = ((totalPosts * 100) / maxPosts);

                double contributionLevel = (percantageLevel * 5) / 100;

                if (contributionLevel == 5D)
                    awards.AppendFormat(string.Format("{0}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", AWARD), "SiteContentImages/TopAward.png", "Superb").Replace(" of 5", string.Empty);

                double totalAwards = Math.Floor(contributionLevel);

                if (totalAwards < 1D)
                    awards.AppendFormat(AWARD, "SiteContentImages/NoBadge.png", Math.Round(contributionLevel, 2));
                else
                    for (int i = 0; i < totalAwards; i++)
                        awards.AppendFormat(AWARD, string.Format("SiteContentImages/Badge{0}.png", (i + 1)), Math.Round(contributionLevel, 2));
            }
            else
            {
                awards.AppendFormat(AWARD, "SiteContentImages/NoBadge.png", maxPosts);
            }

            return awards.ToString();
        }
        
        private static bool ThumbnailCallback() { return false; }
        public static void SaveImageFile(FileUpload fileUploadControl, string imageLocation, string thumbnailLocation, int ratio)
        {
            fileUploadControl.SaveAs(imageLocation);
            fileUploadControl.Dispose();

            System.Drawing.Image image = System.Drawing.Image.FromFile(imageLocation);

            // Start Keep Aspect Ratio
            int thumbWidth = ratio;
            int thumbHeight = image.Height * thumbWidth / image.Width;
            if (image.Height > image.Width)
            {
                thumbHeight = ratio;
                thumbWidth = image.Width * thumbHeight / image.Height;
            }
            // End Keep Aspect Ratio

            System.Drawing.Image thumb = image.GetThumbnailImage(thumbWidth, thumbHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            thumb.Save(thumbnailLocation);
            thumb.Dispose();
            image.Dispose();
        }

        public static void SendRegistrationConfirmationEMail(SiteMember member)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("NoReply@TraditionalMalteseCuisine.com", "Traditional Maltese Cuisine");
            mail.To.Add(new MailAddress(member.EMail));

            StringBuilder body = new StringBuilder();
            body.AppendFormat("Hi {0},\n\n", member);
            body.Append("This is just a confirmation e-mail to let you know that you are now a member of Traditional Maltese Cuisine website.\n\n");
            body.AppendFormat("   > Your login name is: {0}\n", member.Username);
            body.Append("   > Your password is encrypted, therefore it cannot be shown\n\n");
            body.Append("Start contributing now.\n\n");
            body.Append("Enjoy!\n\n");
            body.Append("Best Regards,\n");
            body.Append("Traditional Maltese Cuisine");

            mail.Subject = "Registration Confirmation - Traditional Maltese Cuisine";
            mail.Body = body.ToString();

            SendEMail(mail);
        }

        public static void SendResetPasswordEMail(string email)
        {
            SiteMember member = GetSiteMemberDetailsUsingEMail(email);

            if (member != null)
            {
                member.Password = Guid.NewGuid().ToString();

                UpdateSiteMember(member, SiteMember.PASSWORD_TAG, member.EncryptPassword);

                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("noreply@traditionalmaltesecuisine.com", "Traditional Maltese Cuisine");
                mail.To.Add(new MailAddress(member.EMail));

                StringBuilder body = new StringBuilder();
                body.AppendFormat("Hi {0},\n\n", member);
                body.Append("Recently you requested to reset your password information.\nThis is to inform you that we changed your login details as follows:\n\n");
                body.AppendFormat("   > Your login name is: {0}\n", member.Username);
                body.AppendFormat("   > Your new password is: {0}\n\n", member.Password);
                body.Append("Make sure you change your password from your profile page at your earliest convenience!\n\n");
                body.Append("Enjoy!\n\n");
                body.Append("Best Regards,\n");
                body.Append("Traditional Maltese Cuisine");

                mail.Subject = "Reset Password - Traditional Maltese Cuisine";
                mail.Body = body.ToString();

                SendEMail(mail);
            }
        }

        private static void SendEMail(MailMessage mail)
        {
            SmtpClient smtpClient = new SmtpClient();

            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential("traditionalmaltesecuisine@gmail.com", "traditionalmaltesecuisine123");
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            smtpClient.Send(mail);
        }
        #endregion
    }
}